<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员分组
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'部门列表'),
	array('op'=>'edit','title'=>'添加部门'),
);
//会员分组
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_worker_group',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['usernum']=pdo_count('jy_scrm_member',array('groupid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['qunnum']=0;
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：<red>提示没有权限</red>？应用管理--自建应用--进入应用--【可见范围】--设置部门跟目录
		</br>1、<red>部门名称只是数字</red>？：https://developer.work.weixin.qq.com/document/path/96079 （三方SAAS安装的应用：调用接口或接收通讯录变更事件时，将不再返回除ID（userid和部门ID）之外的其他信息，包括但不限于姓名、手机号、部门名等）
		','edit'=>true,'del_one'=>true,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'部门名称','type'=>'string'),
			array('name'=>'parentid','title'=>'部门上级','type'=>'select','options'=>'bumens','option_value'=>'bumenid'),
			array('name'=>'order','title'=>'部门排序','type'=>'string'),
			//array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_scrm/worker.bumen/upbumen/','title'=>'同步部门'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'部门名称','type'=>'string'),
			//array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['bumens']=$this->getall('worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_worker_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回部门列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'部门名称','name'=>'title','help'=>''),
					array('type'=>'string','title'=>'英文名称','name'=>'name_en','help'=>''),
					array('type'=>'select','title'=>'上级部门','name'=>'parentid','options'=>'groups'),
					array('type'=>'string','title'=>'部门排序','name'=>'order','help'=>''),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),
					
				)
			),
		)
	);
	$return['groups']=pdo_getall('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标签名称',true);
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'name_en'=>trim($postdata['name_en']),
		'status'=>trim($postdata['status']),
		'parentid'=>trim($postdata['parentid']),
		'order'=>trim($postdata['order']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_worker_group',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$data['bumenid']=trim($postdata['bumenid']);
		$this->model('worker')->bumen_updateto($data);
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$bumenid=$this->model('worker')->bumen_createto($data);
		pdo_insert('jy_scrm_worker_group',$data);
		$id=pdo_insertid();
		pdo_update('jy_scrm_worker_group',array('bumenid'=>$bumenid),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择用户组！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_worker_group',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$bumenid=pdo_getcolumn('jy_scrm_worker_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'bumenid');
			if($bumenid){
				$this->model('worker')->bumen_delete($id);
			}
			pdo_delete('jy_scrm_worker_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='upbumen'){
	$this->model('model')->check_apilog('get_worker_bumen',true);
	$res=model('core/wxwork_userapi')->get_worker_bumen();
	if(count($res)<=0){
		exi('该企业微信未设置任何部门！或者《管理组须拥有指定部门的查看权限》才能获取到！','error');	
	}
	$this->model('model')->add_apilog('get_worker_bumen');
	foreach($res as $item){
		$isadd=pdo_getcolumn('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'bumenid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
		$data=array(
			'bumenid'=>$item['id'],
			'title'=>$item['name'],
			'name_en'=>$item['name_en'],
			'parentid'=>$item['parentid'],
			'order'=>$item['order'],
		);
		if($isadd){
			pdo_update('jy_scrm_worker_group',$data,array('merchid'=>$_SESSION['onemerchid'],'bumenid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['bumenid']=$item['id'];
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_scrm_worker_group',$data);
		}
	}
	exi('同步部门成功！');
}